package com.example.sicnuaccountingapp.data.repository

import com.example.sicnuaccountingapp.data.dao.CategoryDao
import com.example.sicnuaccountingapp.data.entity.Category
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
import javax.inject.Singleton
import com.example.sicnuaccountingapp.data.entity.TransactionType

@Singleton
class CategoryRepository @Inject constructor(
    private val categoryDao: CategoryDao
) {
    suspend fun addCategory(category: Category): Long {
        val maxSortOrder = categoryDao.getMaxSortOrder(category.userId, category.type) ?: -1
        return categoryDao.insertCategory(category.copy(sortOrder = maxSortOrder + 1))
    }

    suspend fun updateCategory(category: Category) {
        categoryDao.update(category)
    }

    suspend fun deleteCategory(category: Category) {
        categoryDao.delete(category)
    }

    fun getAllCategories(userId: Long): Flow<List<Category>> {
        return categoryDao.getAllCategories(userId)
    }

    fun getCategoriesByType(userId: Long, type: TransactionType): Flow<List<Category>> {
        return categoryDao.getCategoriesByType(userId, type)
    }

    suspend fun updateCategoryOrder(categories: List<Category>) {
        categoryDao.updateCategoryOrder(categories)
    }
} 